package com.ym.b2b.commons.util;

import java.io.File;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.zc.commons.base.Result;
import com.zc.commons.spring.SpringContextHolder;

/**
 * javaMail发送邮件工具类 待完善 目前只是简单的 发送功能
 * 
 * @author
 * @since
 */
public class MailUtil {

    private Logger logger = Logger.getLogger(getClass());

    /**
     * @Title: sendMail
     * @Description: 发送邮件 方法
     * @Param type 1 存文本 2 html 3带附件
     * @Param toAddress 目标邮件地址
     * @Param mailTitle 邮件标题
     * @Param 邮件内容 mailContent
     * @Return Result
     * @Throws
     */
    public Result sendMail(MailBean mail) {

        Result ret = new Result();

        // check mail input

        JavaMailSenderImpl mailSender = SpringContextHolder.getBean("mailSender");
        MimeMessage mailMessage = mailSender.createMimeMessage();

        try {
            switch (mail.getType()) {
                case 1:
                    sendText(mail, mailSender);
                    break;
                case 2:
                    sendHtml(mail, mailSender);
                    break;
                case 3:
                    sendHtmlAccessory(mail, mailSender);
                    break;
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, "发送邮件失败：" + e.getMessage());
        }
        return ret;

    }

    private void checkInput() {

    }

    /**
     * 发送文本邮件
     * 
     * @Title: sendText
     * @Description: TODO
     * @Param @param mail
     * @Param @param mailSender
     * @Param @throws MessagingException
     * @Return void
     * @Throws
     */
    private void sendText(MailBean mail, JavaMailSenderImpl mailSender) throws MessagingException {
        SimpleMailMessage messageHelper = new SimpleMailMessage();
        messageHelper.setTo(mail.getToAddress());// 接受者
        messageHelper.setFrom(mailSender.getUsername());// 发送者
        messageHelper.setSubject(mail.getMailTitle());// 主题
        // 邮件内容，注意加参数true，表示启用html格式
        messageHelper.setText(mail.getMailContent());
        mailSender.send(messageHelper);
    }

    private void sendHtml(MailBean mail, JavaMailSenderImpl mailSender) throws MessagingException {
        MimeMessage mailMessage = mailSender.createMimeMessage();
        // 设置utf-8或GBK编码，否则邮件会有乱码
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
        messageHelper.setTo(mail.getToAddress());// 接受者
        messageHelper.setFrom(mailSender.getUsername());// 发送者
        messageHelper.setSubject(mail.getMailTitle());// 主题
        // 邮件内容，注意加参数true，表示启用html格式
        messageHelper.setText(mail.getMailContent(), true);
        mailSender.send(mailMessage);
    }

    private void sendHtmlAccessory(MailBean mail, JavaMailSenderImpl mailSender) throws MessagingException,
                                                                                UnsupportedEncodingException {
        MimeMessage mailMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");
        messageHelper.setTo(mail.getToAddress());
        messageHelper.setFrom(mailSender.getUsername());
        messageHelper.setSubject(mail.getMailTitle());

        messageHelper.setText(mail.getMailContent(), true);
        // 附件内容
        messageHelper.addInline("a", new File("E:/xiezi.jpg"));
        messageHelper.addInline("b", new File("E:/logo.png"));
        File file = new File("E:/测试中文文件.rar");
        // 这里的方法调用和插入图片是不同的，使用MimeUtility.encodeWord()来解决附件名称的中文问题
        messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
        mailSender.send(mailMessage);

    }

}
